package com.crm.controller.permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.exception.ServiceException;
import com.crm.pojo.Menubar;
import com.crm.service.permission.MenubarService;
import com.crm.vo.Result;

@Controller
public class MenubarController {

	@Autowired
	private MenubarService menubarService;

	/**
	 * 查询所有功能
	 * 
	 * @return
	 */
	@PostMapping("query-All-Menubars")
	@ResponseBody
	public Map queryAllMenubars(int page, int rows) {
		Long total = menubarService.queryCount();
		List<Menubar> list = menubarService.queryAllMenubar(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 添加功能
	 * @param mname
	 * @return
	 */
	@PostMapping("add-One-Menubar")
	@ResponseBody
	public Result addOneMenubar(String text){
		try {
			Menubar menubar = menubarService.queryOneName(text);
			if(menubar == null){
				menubarService.addMenubar(text);
				return Result.SUCCESS;
			}else{
				return new Result("添加失败，功能名称重复！");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			return new Result("添加失败，功能名称重复！");
		}
	}
	
	@GetMapping("menubartable")
	public String toRoleTable() {
		return "permission/menubar-table";
	}

}
